package com.socialuni.sdk.manage;

import com.socialuni.sdk.model.ProviderLoginQO;
import com.socialuni.sdk.repository.AccountRepository;
import com.socialuni.sdk.constant.platform.PlatformType;
import com.socialuni.sdk.model.UniUnionIdRO;
import com.socialuni.sdk.model.DO.user.AccountDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CommonUserAccountDOGet {
    @Resource
    AccountRepository accountRepository;

    public AccountDO getAccountByUnionId(ProviderLoginQO loginQO, UniUnionIdRO uniUnionIdRO) {
        String provider = loginQO.getProvider();
        String unionId = uniUnionIdRO.getUnionid();
        String platform = loginQO.getPlatform();

        AccountDO accountDO = accountRepository.findByProviderAndUnionId(provider, unionId);

        //根据unionId找不到，代表没有开通开放平台或者没注册
        if (accountDO == null) {
            //通过openId查询试试
            String openId = uniUnionIdRO.getOpenid();
            if (platform.equals(PlatformType.app)) {
                accountDO = accountRepository.findByProviderAndAppOpenId(provider, openId);
            } else {
                accountDO = accountRepository.findByProviderAndMpOpenId(provider, openId);
            }
        }
        return accountDO;
    }
}
